//
//  ScrollWithImage.m
//  PhotoClass
//
//  Created by dllo on 15/11/4.
//  Copyright (c) 2015年 liyinkai. All rights reserved.
//

#import "ScrollWithImage.h"
#import "UIImageView+WebCache.h"
#import "BaseModel.h"
#import "SencondModel.h"
@interface ScrollWithImage()
@property (nonatomic, retain) NSMutableArray *data;
@end
@implementation ScrollWithImage

- (void)dealloc {
//    释放内存
    [super dealloc];
    [_data release];
    [_scrollview release];
}

-(instancetype)initWithFrame:(CGRect)frame withData:(NSArray *)array{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.data = [NSMutableArray arrayWithArray:array];
        _num = array.count;
        [_data insertObject:_data.lastObject atIndex:0];
        [_data addObject:_data[1]];
        
        [self creatScrollView];

    }
    return self;
}

-(void)creatScrollView {
//     创建scrollview
    _scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height)];
    _scrollview.tag = 1001;
    [self addSubview:_scrollview];
    [_scrollview release];
//    设置代理人
    _scrollview.delegate = self;
//    设置整页偏移
    _scrollview.pagingEnabled = YES;
    [_scrollview setContentOffset:CGPointMake(self.bounds.size.width , 0) animated:YES];
    if (_data.count > 0) {
        for (int i = 0; i < _data.count; i++) {
            UIImageView *image = [[UIImageView alloc] initWithFrame:self.bounds];
            image.frame = CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height - 20);
            [_scrollview addSubview:image];
            [image sd_setImageWithURL:[NSURL URLWithString:[_data[i] imgsrc]]];
            [image release];
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width * i + 10, image.frame.size.height, self.frame.size.width - 100, 20)];
            label.font = [UIFont systemFontOfSize:14];
            label.tag = 1000 + i;
            label.text = [_data[i] title];
            [_scrollview addSubview:label];
            [label release];
        }
    }
     //    设置scroll可以偏移的范围
    _scrollview.contentSize = CGSizeMake(self.frame.size.width * _data.count, 0);
    
    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width - 80, self.frame.size.height - 20, 80, 20)];
    page.pageIndicatorTintColor = [UIColor redColor];
    page.currentPageIndicatorTintColor = [UIColor whiteColor];
    page.numberOfPages = _num;
    page.tag = 100001;
    [self addSubview:page];
    [page addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    [page release];
}

- (void) pageChange:(UIPageControl *)page {
    [_scrollview setContentOffset:CGPointMake(self.frame.size.width * (page.currentPage + 1), 0) animated:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == self.bounds.size.width * (_num + 1)) {
        [scrollView setContentOffset:CGPointMake(self.bounds.size.width, 0) animated:NO];
    }
    if (scrollView.contentOffset.x == 0) {
        [scrollView setContentOffset:CGPointMake(self.bounds.size.width * (_num ), 0) animated:NO];
    }
    UIPageControl *page = (UIPageControl *)[self viewWithTag:100001];
    page.currentPage = scrollView.contentOffset.x / self.frame.size.width - 1;
}

#pragma mark - 懒加载
- (NSMutableArray *)data {
    if (!_data) {
        self.data = [NSMutableArray arrayWithCapacity:0];
    }
    return _data;
}
@end
